package com.hyt.it.ogt.kq.main.controller.bm;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.bm.feign.kw.KwClient;
import com.hyt.it.ogt.kq.service.bm.model.entity.SubjectExam;
import com.hyt.it.ogt.kq.service.bm.model.vo.SubjectExamVo;
import com.hyt.it.ogt.kq.service.bm.service.IStudentInfoService;
import com.hyt.it.ogt.kq.service.bm.service.IStudentProjectService;
import com.hyt.it.ogt.kq.service.bm.service.IStudentSubjectService;
import com.hyt.it.ogt.kq.service.bm.service.ISubjectExamHistoryService;
import com.hyt.it.ogt.kq.service.bm.service.ISubjectExamService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liteng
 * @since 2020-06-03
 */
@RestController
@Api(tags = "13.管理端：科目绑定考试接口", value = "科目绑定考试接口")
@ApiSort(value = 13)
@RequestMapping("/bm/subject-exam")
public class SubjectExamController extends BaseController {
	@Autowired
	ISubjectExamService iSubjectExamService;
	@Autowired
	IStudentProjectService iStudentProjectService;
	@Autowired
	IStudentSubjectService iStudentSubjectService;
	@Autowired
	IStudentInfoService iStudentInfoService;
	@Autowired
	KwClient kwClient;
	@Autowired
	ISubjectExamHistoryService iSubjectExamHistoryService;

	@ApiOperation(value = "13.1.1 绑定考试项目", notes = "20000:成功；14046：该考试已绑定其他科目;" + "14047：绑定考试项目数据失败")
	@ApiOperationSort(value = 1)
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Object save(@RequestBody SubjectExam subjectExam) throws Exception {
		QueryWrapper<SubjectExam> wrapper = new QueryWrapper<SubjectExam>();
		wrapper.eq("project_id", subjectExam.getProjectId());
		wrapper.eq("exam_id", subjectExam.getExamId());
		List<SubjectExam> list = iSubjectExamService.list(wrapper);
		boolean flag = true;
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				if (!list.get(i).getSubjectId().equals(subjectExam.getSubjectId())) {
					flag = false;
					break;
				}
			}
			if (!flag) {
				return ApiResponse.
						builder().
						code(Constant.RESPONSE_CODE_31010188).
						data("同一个考试不允许同时绑定2个科目").build();
			}
		}
		wrapper.eq("subject_id", subjectExam.getSubjectId());
		List<SubjectExam> examList = iSubjectExamService.list(wrapper);
		try {
			ApiResponse api = iSubjectExamService.saveSubjectExam(subjectExam, examList);
			return api;
		} catch (Exception e) {
			if (e.getMessage().equals("16239")) {
				return ApiResponse.
						builder().
						code(Constant.RESPONSE_CODE_31010187).
						data("该考试已经开始，无法绑定").build();
			} else {
				return ApiResponse.builder().code(Constant.RESPONSE_CODE_31010186).data("绑定考试失败").build();
			}
		}
	}

	@ApiOperation(value = "13.1.2 查询考试数据", notes = "20000:成功；31010185：查询考试数据失败")
	@ApiOperationSort(value = 2)
	@RequestMapping(value = "/queryExam", method = RequestMethod.GET)
	public Object queryExam(String projectId) {
		ApiResponse api = new ApiResponse();
		try {
			api = kwClient.queryPage(projectId);
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(api.getData()).build();
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_31010185).build();
		}

	}

	@ApiOperation(value = "13.1.2 统计考试数据", notes = "20000:成功；31010184：统计数据失败")
	@ApiOperationSort(value = 3)
	@RequestMapping(value = "/statExam", method = RequestMethod.GET)
	public Object statExam(String projectId) {
		QueryWrapper<SubjectExam> wrapper = new QueryWrapper<SubjectExam>();
		wrapper.eq("project_id", projectId);
		List<SubjectExam> list = iSubjectExamService.list(wrapper);
		ApiResponse api = new ApiResponse();
		List<SubjectExamVo> volist = new ArrayList<SubjectExamVo>();
		try {
			api = kwClient.statExam(projectId);
			if (CollectionUtil.isNotEmpty(list)) {
				for (int i = 0; i < list.size(); i++) {
					SubjectExamVo vo = new SubjectExamVo();
					vo.setExamId(list.get(i).getExamId());
					vo.setExamName(list.get(i).getExamName());
					volist.add(vo);
				}
				if (api.getData() != null) {
					List<Object> examList = (List<Object>) api.getData();
					if (CollectionUtil.isNotEmpty(examList)) {
						for (int i = 0; i < examList.size(); i++) {
							Map<String, Object> tempmap = 
									(Map<String, Object>) examList.get(i);
							for (int j = 0; j < volist.size(); j++) {
								if (volist.get(j).getExamId().equals(
										tempmap.get("examId").toString())) {
									if (tempmap.get("onlineNumber") != null) {
										volist.get(j).setOnlineNumber(
											Integer.valueOf(
											tempmap.get("onlineNumber").
											toString()).intValue());
									}
									if (tempmap.get("completeNumber") != null) {
										volist.get(j).setCompleteNumber(
											Integer.valueOf(
											tempmap.get("completeNumber").
											toString()).intValue());
									}
									if (tempmap.get("gradeState") != null) {
										volist.get(j).setGradeState(
											Integer.valueOf(
											tempmap.get("gradeState").
											toString()).intValue());
									}
									if (tempmap.get("examState") != null) {
										volist.get(j).setExamState(
											Integer.valueOf(
											tempmap.get("examState")
											.toString()).intValue());
									}
									if (tempmap.get("totalNumber") != null) {
										volist.get(j).setTotalNumber(
											Integer.valueOf(
											tempmap.get("totalNumber")
											.toString()).intValue());
									}
								}
							}
						}
					}
				}
			}
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(volist).build();
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_31010184).build();
		}
	}
}
